import re
import json
import os
from datetime import datetime

# 目标JSON文件路径
json_file_path = 'D:\pythonProject\虎牙\wenjian\ALL_LIST_DATA.json'

arrList = []

# 确保目标文件夹存在
os.makedirs(os.path.dirname(json_file_path), exist_ok=True)

# 你想保留的字段列表
fields_keep = ["sNick", "sScreenshot", "iStartTime"]

# 读取HTML文件内容
with open('wenjian/huya.html', 'r', encoding='utf-8') as file:
    html_content = file.read()

# 使用正则表达式查找ALL_LIST_DATA变量的值
pattern = r'var ALL_LIST_DATA = (\[.*?\]);'
match = re.search(pattern, html_content, re.DOTALL)


def filter_fields(data, fields_to_keep):
    # 过滤字段
    filtered_datas = []
    for item in data:
        filtered_item = {key: item[key] for key in fields_to_keep}
        # 转换时间戳
        filtered_item["iStartTime"] = datetime.fromtimestamp(filtered_item["iStartTime"]).strftime('%Y-%m-%d %H:%M:%S')
        # 添加排序字段
        filtered_datas.append(filtered_item)

    # 根据 iStartTime 排序并添加 order 字段
    filtered_datas.sort(key=lambda x: datetime.strptime(x["iStartTime"], '%Y-%m-%d %H:%M:%S'))
    for index, item in enumerate(filtered_datas):
        item["order"] = index + 1

    return filtered_datas


if match:
    # 提取匹配到的内容，并去掉前后的方括号和可能的空白字符
    json_str = match.group(1).strip()

    # JSON和Python的字典/列表结构相似，可直接使用json库来解析
    try:
        all_list_data = json.loads(json_str)
        # 打印解析后的字典
        # print(all_list_data)
    except json.JSONDecodeError as e:
        print(f"Error decoding JSON: {e}")
else:
    print("ALL_LIST_DATA not found in the HTML file.")

# 使用函数过滤字段
filtered_data = filter_fields(all_list_data, fields_keep)
# 将Python对象存储为JSON文件
with open(json_file_path, 'w', encoding='utf-8') as json_file:
    json.dump(filtered_data, json_file, ensure_ascii=False, indent=4)
